/**
 * Created by inZa on 2. 10. 2014.
 */

var filterModule = angular.module('aaser.filters', []);

filterModule.filter('ClassificationFilter', function () {
    return function (parent, childName) {
        if (parent != null && childName != null) {

            switch (childName) {

                case 'region':
                    return parent.regions;
                    break;
                case 'site':
                    return parent.sites;
                    break;
                case 'product':
                    return parent.products;
                    break;
                case 'technology':
                    return parent.technologies;
                    break;
                case 'team':
                    return parent.teams;
                    break;
                case 'cell':
                    return parent.cells;
                    break;
                case 'machine':
                    return parent.machines;
                    break;

            }
        }
        return [];
    }
});


filterModule.filter('ldapAssignmentSearch', ['$filter', function ($filter) {
    return function (entities, input, assigned) {
        if (angular.isDefined(input) && angular.isDefined(assigned)) {
            input = input.toLowerCase();
            var filtered = [];
            angular.forEach(entities, function (entity) {
                if (entity.name.toLowerCase().indexOf(input) >= 0 && $filter('filter')(assigned, entity, true).length < 1) filtered.push(entity);
            });
            return filtered;
        }
        if (angular.isDefined(assigned)) {
            var filtered = [];
            angular.forEach(entities, function (entity) {
                if ($filter('filter')(assigned, entity.name, true).length < 1) filtered.push(entity);
            });
            return filtered;
        }
        if (angular.isDefined(input)) {
            input = input.toLowerCase();
            var filtered = [];
            angular.forEach(entities, function (entity) {
                if (entity.name.toLowerCase().indexOf(input) >= 0) filtered.push(entity);
            });
            return filtered;
        }
        return entities;
    }
}]);

filterModule.filter('ldapEntitySearch', ['$filter', function ($filter) {
    return function (entities, input, selected) {
        if (input) {
            if (angular.isDefined(selected)) {
                input = input.toLowerCase();
                var filtered = [];
                if (!angular.isArray(selected)) {
                    selected = new Array(selected);
                }
                angular.forEach(entities, function (entity) {
                    if (entity.name.toLowerCase().indexOf(input) >= 0 && $filter('filter')(selected, {name: entity.name}, true).length < 1) filtered.push(entity);
                });
                return selected.concat(filtered);

            } else {
                input = input.toLowerCase();
                var filtered = [];
                angular.forEach(entities, function (entity) {
                    if (entity.name.toLowerCase().indexOf(input) >= 0)  filtered.push(entity);
                });
                return filtered;
            }
        }
        return entities;
    }
}]);



